auto()
if (!requestScreenCapture(true)) {
  console.log('请求截图失败')
  toast('请求截图失败')
  exit()
}
sleep(3000)
var screenWidth = device.width // 分辨率
var screenHeight = device.height
console.log('screenHeight', screenHeight)
console.log('screenWidth', screenWidth)
const allScreen = [2160, 2340, 2400, 2712]
// if(!allScreen.includes(screenWidth)){
//   screenHeight = 2400
//   screenWidth = 1080
// }
// sleep(2000)

const points = require(`./${screenHeight}x${screenWidth}.js`)
console.log('points')

setScreenMetrics(screenWidth, screenHeight)
function clickFun(point, sleepTime) {
  if (!sleepTime) {
    sleepTime = 0.9
  }
  const pointX = Math.floor(point[0] + (2 - Math.random() * 4))
  const pointY = Math.floor(point[1] + (2 - Math.random() * 4))
  click(pointX, pointY)
  const st = sleepTime * 1000 + Math.random() * 500
  sleep(st)
}
function move(dir, time) {
  const center = [380, 850] // 移动中心
  const dirObj = {
    l: [80, 850],
    r: [800, 850],
    t: [380, 500],
    d: [380, 980],
    lt: [225, 696],
    ld: [200, 980],
    rt: [567, 714],
    rd: [500, 980]
  }
  const centerX = center[0]
  const centerY = center[1]
  const toX = dirObj[dir][0]
  const toY = dirObj[dir][1]
  const dur = time * 1000 - 150
  press(centerX, centerY, 150)
  swipe(centerX, centerY, toX, toY, dur)
}
function randomMove(time) {
  var allTime = time
  const allDirect = ['l', 'r', 't', 'd', 'lt', 'ld', 'rt', 'rd']
  var dir = 0
  while (allTime > 0) {
    var randomDir = allDirect[dir % 8]
    var randomTime = Math.floor(Math.random() * 2 + 1)
    move(randomDir, randomTime)
    allTime -= randomTime
    dir++
  }
}

function didida(time) {
  console.log('滴滴答开始')
  clickFun(points.taskFirst, time)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.secSel)
  clickFun(points.skip)
  clickFun(points.thirdSel)
  clickFun(points.skip)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.secSel)
  clickFun(points.skip)
  clickFun(points.thirdSel)
  clickFun(points.skip)
  clickFun(points.blank)
  console.log('滴滴答结束')
  autoAccept(25)
}
function doAnswer() {
  clickFun(points.commit)
  clickFun(points.skip)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.blank)
  clickFun(points.secSel)
  clickFun(points.skip)
  clickFun(points.thirdSel)
  clickFun(points.skip)
  clickFun(points.blank)
}
function doCaiji(time) {
  clickFun(points.taskFirst, time)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.taskFirst, time)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.commit)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.skip)
  clickFun(points.commit)
  clickFun(points.skip)
}
function gaoliang(time) {
  clickFun(points.skip, time)
  clickFun(points.skip)
  console.log('采集高粱开始')
  doCaiji(25)
  console.log('采集高粱结束')
  autoAccept(45)
}
function xiaomai(time) {
  clickFun(points.skip, time)
  clickFun(points.skip)
  console.log('采集小麦开始')
  doCaiji(12)
  console.log('采集小麦结束')
  autoAccept(12)
}
function doXunbao(time) {
  clickFun(points.taskFirst, time)
  clickFun(points.close)
  clickFun(points.taskFirst, 10)
  clickFun(points.close)
  clickFun(points.taskFirst, 10)
  clickFun(points.close)
  clickFun(points.taskFirst, 10)
  clickFun(points.close)
  clickFun(points.taskFirst, 10)
  clickFun(points.close)
  clickFun(points.taskFirst, time)
  clickFun(points.skip)
  clickFun(points.blank)
}
function gongfa(time) {
  clickFun(points.skip, time)
  clickFun(points.skip)
  console.log('工法寻宝开始')
  doXunbao(25)
  console.log('工法寻宝结束')
  autoAccept(12)
}
function jieqi(time) {
  clickFun(points.skip, time)
  clickFun(points.skip)
  console.log('节气寻宝开始')
  doXunbao(45)
  console.log('节气寻宝结束')
  autoAccept(28)
}
function daocao(time) {
  console.log('割稻草开始')
  clickFun(points.skip, time)
  clickFun(points.skip)
  clickFun(points.taskFirst, 4)
  clickFun(points.skip)
  clickFun(points.secSel)
  clickFun(points.challenge, 5)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank, 3)
  move('t', 2)
  move('rt', 2)
  randomMove(140)
  clickFun(points.taskFirst, 20)
  clickFun(points.taskFirst, 5)
  clickFun(points.taskFirst, 5)
  clickFun(points.taskFirst, 5)
  clickFun(points.taskFirst, 5)
  clickFun(points.skip)
  clickFun(points.blank)
  clickFun(points.submit)
  clickFun(points.blank)
  clickFun(points.taskFirst)
  clickFun(points.skip)
  clickFun(points.blank)
  console.log('割稻草结束')
  autoAccept(65)
}

function zoujie(time) {
  console.log('走街串巷开始')
  clickFun(points.skip, time)
  clickFun(points.firSel)
  clickFun(points.skip)
  console.log('第1次')
  dozoujie()
  console.log('第2次')
  dozoujie()
  console.log('第3次')
  dozoujie()
  console.log('第4次')
  dozoujie()
  console.log('第5次')
  dozoujie()
  console.log('走街串巷结束')
  autoAccept()
}
function dozoujie() {
  clickFun(points.commit)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.taskFirst, 60)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.taskFirst, 60)
  clickFun(points.skip)
  clickFun(points.blank)
}
function acceptShengchanxian() {
  clickFun(points.task1Go, 2)
  clickFun(points.task1Go, 2)
  clickFun(points.game1, 2)
  clickFun(points.game2, 2)
  clickFun(points.game3, 2)
  clickFun(points.gameSelect, 2)
  clickFun(points.gameSwitch, 2)
  shengchanxian(50)
}
function shengchanxian(time) {
  console.log('生产线开始')
  clickFun(points.taskFirst, time)
  clickFun(points.skip)
  clickFun(points.skip)
  clickFun(points.skip)
  clickFun(points.secSel, 1)
  console.log('小麦研磨')
  gameDoEnd() // 小麦
  clickFun(points.taskFirst, 16)
  clickFun(points.skip)
  clickFun(points.skip)
  clickFun(points.secSel, 1)
  console.log('制曲')
  gameDoEnd() // 制曲
  clickFun(points.taskFirst, 55)
  clickFun(points.skip)
  clickFun(points.skip)
  clickFun(points.secSel, 1)
  console.log('消消乐')
  gameDoEnd()
  clickFun(points.autoUseGood) // 自动使用基酒
  console.log('生产线结束')
  clickFun(points.skip)
  clickFun(points.richeng)
  clickFun(points.task1Go)
  dati()
}
function gameDoEnd() {
  clickFun(points.gameStart, 2)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank, 4)
  clickFun(points.gameBack)
  clickFun(points.gameConfirm, 5)
  clickFun(points.gameEnd, 2)
}
function dati() {
  console.log('巽风达人开始')
  clickFun(points.blank)
  for (let index = 0; index < 11; index++) {
    clickFun(points.datiAnswer, 4) // 选项1
  }
  clickFun(points.datiClose) // 关闭弹窗
  checkSecond()
}
function checkSecond() {
  home()
  sleep(2000)
  if (doSecondApp) {
    doSecondApp = false
    openApp(2)
  } else {
    home()
    engines.stopAll()
    exit()
  }
}
function wenda(time) {
  console.log('知识问答开始')
  clickFun(points.taskFirst, time)
  clickFun(points.firSel)
  clickFun(points.skip)
  doAnswer()
  clickFun(points.blank)
  clickFun(points.commit)
  clickFun(points.skip)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.blank)
  doAnswer()
  console.log('知识问答结束')
  autoAccept(45)
}

function checkTaskOver() {
  const text = imageClipOcr(points.ocrRichengBtn5)
  if (text === '前往') {
    return false
  } else {
    clickFun(points.receive)
    if (doShengchanxian) {
      acceptShengchanxian()
    } else {
      home()
      engines.stopAll()
      exit()
    }
    return true
  }
}
function autoAcceptUndo(time) {
  clickFun(points.goAllTask)
  clickFun(points.canAccept)
  const taskUndoName = imageClipOcr(points.canDoTaskText, true)
  if (taskUndoName) {
    clickFun(points.toNowCanDo, time)
    clickFun(points.firSel)
    clickFun(points.skip, 1)
    doNowFun()
  } else {
    clickFun(points.allTaskClose)
    clickFun(points.richeng)
    const isHasUn = imageClipOcr(points.richengTask3Text, true)
    if (isHasUn === '前往') {
      clickFun(points.task3Go, time)
      doNowFun()
    } else {
      clickFun(points.task1Go)
      exit()
    }
  }
}
var taskMain = 3
function autoAccept(time) {
  var taskText = ''
  clickFun(points.skip)
  clickFun(points.richeng)
  console.log('taskMain', taskMain)
  if (taskMain === 5) {
    if (checkTaskOver()) return
  }
  taskText = imageClipOcr(points['ocrRichengText' + taskMain])
  console.log('识别日程任务区文字', taskText)
  if (taskText.includes('共舞') || taskText.includes('净')) {
    taskMain++
    taskText = imageClipOcr(points['ocrRichengText' + taskMain])
    if (taskText.includes('净')) {
      taskMain++
      taskText = '走街串巷'
      if (checkTaskOver()) return
    }
  }
  clickFun(points.taskBtn[taskMain - 1], time || 30)
  clickFun(points.close)
  clickFun(points.submit)
  clickFun(points.firSel)
  clickFun(points.skip, 1)
  autoTask(taskText)
}
var autoTask = function (text) {
  console.log('text', text)
  if (text.includes('每日')) {
    didida(3)
  } else if (text.includes('野生') || text.includes('调研')) {
    if (text.includes('小')) {
      xiaomai(2)
    } else {
      gaoliang(2)
    }
  } else if (text.includes('工法') || text.includes('寻宝')) {
    if (text.includes('气')) {
      jieqi(2)
    } else {
      gongfa(2)
    }
  } else if (text.includes('草') || text.includes('稻草')) {
    daocao(2)
  } else if (text.includes('走') || text.includes('走街')) {
    zoujie(2)
  } else if (text.includes('酿酒')) {
    taskMain = 5
    autoAccept(2)
  } else if (text.includes('净')) {
    taskMain++
    autoAccept(2)
  } else if (text.includes('知识')) {
    wenda(2)
  } else if (text.includes('资格')) {
    autoAccept(30)
  } else {
    clickFun(points.skip)
    clickFun(points.close)
    autoAccept()
  }
}
function doNowFun() {
  autoTask(imageClipOcr(points.ocrMainTask))
}
var imageClipOcr = function (point) {
  // sleep(2000)
  var cp = images.captureScreen()
  var clip = images.clip(cp, point[0], point[1], point[2], point[3])
  var text = ocr(clip)
  // toast('识别文字: ' + text)
  return text
}

function ocr(img) {
  // 识别图片中的文字，返回完整识别信息（兼容百度OCR格式）。
  //可选语言：拉丁 "la" , 中文 "zh" ,梵文 "sa" ,日语 "ja" , 韩语 "ko"
  let result = gmlkit.ocr(img, 'zh')
  //排序 sort()会改变原对象，sorted() 不会改变原对象，而是返回新对象
  result.sort()
  //或者
  //let newResult = result.sorted()
  console.log('文本识别信息: ' + result.text)
  // 回收图片
  img.recycle()
  return result.text
}

function getScreenDirection() {
  importPackage(android.content)
  return context.getResources().getConfiguration().orientation
}
function openApp(num) {
  if (getScreenDirection() === 2 && !num) {
    taskMain = 3
    // doSecondApp = false
    doShengchanxian = false
    autoAccept()
    return
  }
  app.launchApp('巽风')
  sleep(2000)
  if (!num) {
    clickFun(points.openApp1, 10)
  } else {
    doShengchanxian = true
    clickFun(points.openApp2, 10)
  }
  clickFun(points.openAppNoticeClose, 3)
  clickFun(points.openAppNoticeClose, 5)
  clickFun(points.submit, 3)
  clickFun(points.openAppNoticeClose, 5)
  clickFun(points.openAppClose, 5)
  clickFun(points.openAppTaskClose, 3)
  clickFun(points.openAppClose, 5)
  clickFun(points.openAppTaskClose, 3)
  taskMain = 3
  autoAccept()
}
var doSecondApp = true
var doShengchanxian = true
openApp()
